package com.ctshk.rpc.order.hotel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.hotel.dto.*;
import com.ctshk.rpc.hotel.req.HotelPayOrderCollectionReq;
import com.ctshk.rpc.hotel.service.IHotelOrderCollectionService;
import com.ctshk.rpc.order.hotel.entity.*;
import com.ctshk.rpc.order.hotel.mapper.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 酒店收款明细 服务实现类
 * </p>
 *
 * @author yinhuan
 * @since 2021-03-08
 */
@DubboService
public class HotelOrderCollectionServiceImpl extends ServiceImpl<HotelOrderCollectionMapper, HotelOrderCollection> implements IHotelOrderCollectionService {
    @Autowired
    private HotelOrderMapper hotelOrderMapper;
    @Autowired
    private HotelOrderTouristMapper hotelOrderTouristMapper;
    @Autowired
    private HotelOrderCollectionMapper hotelOrderCollectionMapper;
    @Autowired
    private HotelManualOrderMapper hotelManualOrderMapper;
    @Autowired
    private HotelOrderRefundMapper hotelOrderRefundMapper;

    /**
     * 收款再次确认
     *
     * @param collectionId 收款明细id
     *         userId 用户id
     * @return 返回值
     */
    @Override
    public Result confirm(Long collectionId, Long userId) {
        HotelOrderCollection hotelOrderCollection = hotelOrderCollectionMapper.selectById(collectionId);
        if(Objects.isNull(hotelOrderCollection)){
            return Result.failed(SystemError.HOTEL_10034);
        }
        hotelOrderCollection.setStatus(1);
        hotelOrderCollection.setGmtModified(LocalDateTime.now());
        hotelOrderCollection.setModifiedId(userId);
        hotelOrderCollectionMapper.updateById(hotelOrderCollection);
        return Result.success();
    }

    @Override
    public Result<HotelOrderDetailDTO> queryDetail(Long orderId, Long userId) {
        //根据订单id查询订单信息
        HotelOrder hotelOrder = hotelOrderMapper.selectById(orderId);
        if(Objects.isNull(hotelOrder)){
            return Result.failed(SystemError.HOTEL_10031);
        }
        HotelOrderDetailDTO hotelOrderDetailDTO =  EntityUtil.copy(hotelOrder,HotelOrderDetailDTO.class);

        //酒店外挂单
        HotelManualOrder hotelManualOrder = hotelManualOrderMapper.selectOne(new QueryWrapper<HotelManualOrder>().lambda().eq(HotelManualOrder::getOrderId, orderId));
        if(!Objects.isNull(hotelManualOrder)){
            HotelManualOrderDetailDTO hotelManualOrderDetailDTO =  EntityUtil.copy(hotelManualOrder,HotelManualOrderDetailDTO.class);
            hotelOrderDetailDTO.setHotelManualOrderDetailDTO(hotelManualOrderDetailDTO);
        }

        //酒店订单旅客信息
        List<HotelOrderTourist> hotelOrderTouristList = hotelOrderTouristMapper.selectList(new QueryWrapper<HotelOrderTourist>().lambda().eq(HotelOrderTourist::getOrderId, orderId));
        if(!CollectionUtils.isEmpty(hotelOrderTouristList)){
            List<HotelOrderTouristDTO> hotelOrderTouristDTOList =  EntityUtil.copyList(hotelOrderTouristList,HotelOrderTouristDTO.class);
            hotelOrderDetailDTO.setHotelOrderTouristDTOList(hotelOrderTouristDTOList);
        }

        //酒店收款明细信息
        List<HotelOrderCollection> hotelOrderCollectionList = hotelOrderCollectionMapper.selectList(new QueryWrapper<HotelOrderCollection>().lambda().eq(HotelOrderCollection::getOrderId, orderId));
        if(!CollectionUtils.isEmpty(hotelOrderCollectionList)){
            List<HotelOrderCollectionDTO> hotelOrderCollectionDTOList =  EntityUtil.copyList(hotelOrderCollectionList,HotelOrderCollectionDTO.class);
            hotelOrderDetailDTO.setHotelOrderCollectionDTOList(hotelOrderCollectionDTOList);
        }

        //酒店退款明细信息
        List<HotelOrderRefund> hotelOrderRefundList = hotelOrderRefundMapper.selectList(new QueryWrapper<HotelOrderRefund>().lambda().eq(HotelOrderRefund::getOrderId, orderId));
        if(!CollectionUtils.isEmpty(hotelOrderRefundList)){
            List<HotelOrderRefundDTO> hotelOrderRefundDTOList =  EntityUtil.copyList(hotelOrderRefundList,HotelOrderRefundDTO.class);
            hotelOrderDetailDTO.setHotelOrderRefundDTO(hotelOrderRefundDTOList);
        }

        return Result.success(hotelOrderDetailDTO);
    }

    @Override
    public Result update(HotelPayOrderCollectionReq collectionReq, Long userId) {
        HotelOrderCollection hotelOrderCollection = hotelOrderCollectionMapper.selectById(collectionReq.getId());
        if(Objects.isNull(hotelOrderCollection)){
            return Result.failed(SystemError.HOTEL_10034);
        }
        hotelOrderCollection.setRemark(collectionReq.getRemark());
        hotelOrderCollectionMapper.updateById(hotelOrderCollection);
        return Result.success();
    }
}
